package com.syezon.wifikey.redbag.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.hodanet.appadvplatclient.ReportAgent;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.R;
import com.syezon.wifikey.ad.AdPositionEnum;
import com.syezon.wifikey.ad.AdUtils;
import com.syezon.wifikey.channel.ChannelConfig;
import com.syezon.wifikey.imageloader.ImageLoader;
import com.syezon.wifikey.redbag.info.BoxInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/10/30.
 */

public class BoxAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<BoxInfo> mData = new ArrayList<>();

    public BoxAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<BoxInfo> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    public List<BoxInfo> getData() {
        return mData;
    }

    public void addData(BoxInfo boxInfo) {
        if (boxInfo != null) {
            mData.add(boxInfo);
            notifyItemInserted(mData.size());
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == BoxInfo.TYPE_TITLE) {
            return new TitleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_box_title, parent, false));
        }
        if (viewType == BoxInfo.TYPE_AD_TOOLS) {
            return new AdSmallViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_box_ad_small, parent, false));
        }
        if (viewType == BoxInfo.TYPE_AD_PIC_TEXT) {
            return new AdTextPicViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_box_ad_text_pic, parent, false));
        }
        if (viewType == BoxInfo.TYPE_DIVIDER) {
            return new CategoryDividerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_box_divider, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder != null) {
            BoxInfo boxInfo = mData.get(position);
            int type = boxInfo.getType();
            String title = boxInfo.getTitle();
            AdInfo adInfo = boxInfo.getAdInfo();
            if (type == BoxInfo.TYPE_TITLE && holder instanceof TitleViewHolder) {
                TitleViewHolder viewHolder = (TitleViewHolder) holder;
                onBindTitleViewHolder(viewHolder, title);
            }
            if (type == BoxInfo.TYPE_AD_TOOLS && holder instanceof AdSmallViewHolder && adInfo != null) {
                AdSmallViewHolder viewHolder = (AdSmallViewHolder) holder;
                onBindSmallAdViewHolder(viewHolder, adInfo);
            }
            if (type == BoxInfo.TYPE_AD_PIC_TEXT && holder instanceof AdTextPicViewHolder && adInfo != null) {
                AdTextPicViewHolder viewHolder = (AdTextPicViewHolder) holder;
                onBindTextPicAdViewHolder(viewHolder, adInfo);
            }
            if (type == BoxInfo.TYPE_DIVIDER) {

            }
        }
    }

    private void onBindTextPicAdViewHolder(AdTextPicViewHolder viewHolder, final AdInfo adInfo) {
        ImageLoader.getInstance().displayImage(mContext, adInfo.getPic(), viewHolder.ivAd);
        viewHolder.tvAdName.setText(adInfo.getName());
        viewHolder.tvAdDes.setText(adInfo.getDesc());
        viewHolder.tvAdRec.setText(adInfo.getDesc());
        if (false) {
            viewHolder.tvAdDes.setVisibility(View.GONE);
            viewHolder.tvAdRec.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvAdDes.setVisibility(View.VISIBLE);
            viewHolder.tvAdRec.setVisibility(View.GONE);
        }
        if (!adInfo.isReportExtShow()) {
            ReportAgent.reportAdvExtShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
            adInfo.setReportExtShow(true);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdUtils.adClicked(mContext, adInfo, AdPositionEnum.BOX_AD_2);
            }
        });
    }

    private void onBindSmallAdViewHolder(AdSmallViewHolder viewHolder, final AdInfo adInfo) {
        ImageLoader.getInstance().displayImage(mContext, adInfo.getPic(), viewHolder.ivAd);
        viewHolder.tvAdName.setText(adInfo.getName());
        if (!adInfo.isReportExtShow()) {
            ReportAgent.reportAdvExtShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
            adInfo.setReportExtShow(true);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdUtils.adClicked(mContext, adInfo, AdPositionEnum.BOX_AD_1);
            }
        });
    }


    private void onBindTitleViewHolder(TitleViewHolder viewHolder, String title) {
        viewHolder.tvTitle.setText(title);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        BoxInfo boxInfo = mData.get(position);
        return boxInfo.getType();
    }

    class TitleViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView tvTitle;

        public TitleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class CategoryDividerViewHolder extends RecyclerView.ViewHolder {

        public CategoryDividerViewHolder(View itemView) {
            super(itemView);
        }
    }

    class AdSmallViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_ad)
        ImageView ivAd;
        @BindView(R.id.tv_ad)
        TextView tvAdName;

        public AdSmallViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class AdTextPicViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_ad)
        ImageView ivAd;
        @BindView(R.id.tv_ad_title)
        TextView tvAdName;
        @BindView(R.id.tv_ad_des)
        TextView tvAdDes;
        @BindView(R.id.tv_ad_des_rec)
        TextView tvAdRec;

        public AdTextPicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
